﻿/************************************************************************************
 * xujiajun@yoozoo.com 御剑
 * 基于相机视野范围左下和右上的容器策略
 ***********************************************************************************/

using WorldMapBase.Camera;
using WorldMapBase.Layer;
using Unity.Mathematics;
using UnityEngine;

namespace WorldMapBase
{
    /// <summary>
    /// 基于相机视野范围左下和右上的容器策略
    /// </summary>
    public class CameraLayerContainerStrategy : ILayerContainerStrategy
    {
        /// <summary>
        /// 视图范围
        /// </summary>
        private int4 m_ViewRect;

        /// <summary>
        /// 用于计算视图范围时的缩放系数
        /// </summary>
        private float m_ViewRectScaleRatio;

        /// <summary>
        /// 相机对象
        /// </summary>
        private ICamera m_Camera;

        /// <summary>
        /// 构造基于相机视野范围的容器策略
        /// </summary>
        /// <param name="camera">相机对象</param>
        /// <param name="viewRectScaleRatio">用于计算视图范围时的缩放系数</param>
        public CameraLayerContainerStrategy(ICamera camera, float viewRectScaleRatio = 1)
        {
            m_Camera = camera;
            m_ViewRectScaleRatio = viewRectScaleRatio;
        }

        public int CompareLayer(int layer1, int layer2)
        {
            return layer1 < layer2 ? -1 : (layer1 > layer2 ? 1 : 0);
        }

        public int4 GetViewRect()
        {
            var corner = m_Camera.GetCameraCorner();
            var width = Mathf.Ceil(corner.z - corner.x);
            var height = Mathf.Ceil(corner.w - corner.y);
            //var centerX = corner.x + (width / 2);
            //var centerY = corner.y + (height / 2);
            width =(width * m_ViewRectScaleRatio);
            height = (height * m_ViewRectScaleRatio);
            //var halfWidth = width/2;
            //var halfHeight = height/2;
            int left = Mathf.FloorToInt(corner.x) - Mathf.CeilToInt(width / 2);
            int bottom = Mathf.FloorToInt(corner.y) - Mathf.CeilToInt(height / 2);
            int right = Mathf.CeilToInt(corner.z) + Mathf.CeilToInt(width / 2);
            int top = Mathf.CeilToInt(corner.w) + Mathf.CeilToInt(height / 2);
            m_ViewRect.x = left;
            m_ViewRect.y = bottom;
            m_ViewRect.z = right - left;
            m_ViewRect.w = top - bottom;

            //UnityEngine.Debug.Log("xxxxxxxxxxxxxxxxxxxxx");
            //UnityEngine.Debug.Log("corner.x " + corner.x);
            //UnityEngine.Debug.Log("corner.y " + corner.y);
            //UnityEngine.Debug.Log("width " + width);
            //UnityEngine.Debug.Log("height " + height);
            //UnityEngine.Debug.Log("centerX " + centerX);
            //UnityEngine.Debug.Log("centerY " + centerY);
            //UnityEngine.Debug.Log("halfWidth " + halfWidth);
            //UnityEngine.Debug.Log("halfHeight " + halfHeight);
            //UnityEngine.Debug.Log("m_ViewRect.x " + m_ViewRect.x);
            //UnityEngine.Debug.Log("m_ViewRect.y " + m_ViewRect.y);
            //UnityEngine.Debug.Log("m_ViewRect.z " + m_ViewRect.z);
            //UnityEngine.Debug.Log("m_ViewRect.w " + m_ViewRect.w);

            return m_ViewRect;
        }
    }

}
